home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / pi-5ways.zip / PINDROP.C < prev    next >
C/C++ Source or Header  |  1991-06-15  |  4KB  |  124 lines

  1. /**************************************************************************/
  2. /*         Calculate π by  dropping a pin on a wood plank floor.          */      
  3. /*       If any part of the pin touches the "crack" between boards,       */
  4. /*                            it is a HIT.                                */
  5. /*    π is calculated from the ratio of hits to total drops of the pin.   */
  6. /*                                                                        */
  7. /*                           Mendel Cooper                                */
  8. /*                          3138 Foster Ave.                              */
  9. /*                         Baltimore, MD 21224                            */
  10. /*                                                                        */
  11. /*                                06/91                                   */
  12. /*             Source code placed in the public domain.                   */
  13. /**************************************************************************/
  14.  
  15.  
  16.  
  17. /* May need #include <math.h> */
  18. #define MAX 32000
  19.  
  20. /*********************************************************************/
  21. /*                            GET_SEED                               */  
  22. /*     Returns 4-digit random seed from BIOS time (up to 5959)       */
  23. /*********************************************************************/
  24.  
  25. int get_seed()
  26.  
  27. {
  28. char  *time, *gettime(),time$[5];
  29. int j;
  30.  
  31. time = gettime();
  32.  
  33. *(time$) = *(time + 3);
  34. *(time$ + 1) = *(time + 4);
  35. *(time$ + 2) = *(time + 6);
  36. *(time$ + 3) = *(time + 7);
  37. *(time$ + 4) = '\0';
  38.  
  39.      j = atoi(time$);
  40.  
  41.      return (j);
  42.  
  43. }
  44.  
  45. /**********************************************************************/
  46. /*                           GETRAND()                                */
  47. /*               Outputs random number (0.0 - 1.0)                    */
  48. /**********************************************************************/
  49.  
  50. double getrand()
  51.  
  52. {
  53. int random;   /*number generated by rand() function*/
  54. double scale_factor = 32768.0;   /*scaling factor*/
  55. double dx;         /*Random number between 0.0 and 1.0 returned by fn*/
  56.                    /* X-coordinate of one end of the dropped pin */
  57.  
  58. srand(get_seed());  /*seeds random generator*/
  59.  
  60. random = rand();
  61.  
  62. dx = (double)random / scale_factor;
  63.  
  64. return (dx);
  65. }
  66.  
  67.  
  68. /**********************************************************************/
  69. /*                           GETANGLE()                               */
  70. /*               Outputs random number (0.0 - 180.0)                  */
  71. /**********************************************************************/
  72.  
  73. double getangle()
  74.  
  75. {
  76. int random;   /*number generated by rand() function*/
  77. double scale_factor = 182.039;   /*scaling factor*/
  78. double angle;      /*Random number between 0.0 and 180.0 returned by fn*/
  79.                    /* Angle to the horizontal of the dropped pin */
  80.  
  81. srand(get_seed());  /*seeds random generator*/
  82.  
  83. random = rand();
  84.  
  85. angle = (double)random / scale_factor;
  86.  
  87. return (angle);
  88. }
  89.  
  90. /**************************************************************************/
  91.  
  92. main()
  93. {
  94. double cos(), getrand(), getangle();
  95. double Dx, theta, ratio, Pi;
  96. int hits = 0, drops = 0, getseed();
  97.  
  98.  
  99. clrscrn();       /* Clears screen.  You may delete this line if error. */
  100.  
  101.  
  102. while (drops < MAX) {      /*integer value, still...*/
  103.  
  104. drops++;
  105.  
  106. Dx =  getrand();   /*Generate horizontal position of one end of the pin*/
  107. theta = getangle();   /* Angle of the pin from the horizontal */
  108.  
  109. if (theta <= 90.0)
  110.    if ( Dx + cos(theta) >= 1)
  111.       hits++;
  112.  
  113. if (theta > 90.0)
  114.    if ( (Dx - cos (180.0 - theta)) <= 0 )
  115.       hits++;
  116.  
  117. ratio = (double)hits / (double) drops;
  118.  
  119. printf("Drops = %5d   ---   Hits = %5d               ============>  π ≈ %f \n",
  120.         drops, hits, 10.0 * ratio);
  121.  
  122. }
  123. }
  124.